/**
 * 通用的对分页的预处理逻辑.
 * @param curPageIndex 当前页码, 基于0
 * @param curPageNum 当前页容量
 * @param totalSize 总数
 * @param pageIndexName 页码的参数名, 默认为pageIndex. 该参数用于GET中
 * @param pageNumName 页容量的参数名, 同上
 * @param onclick 当切换页码/页容量时的出发逻辑. 可以为空, 为空则默认改变当前url. 其原型为function(pageindex, pagenum)
 * @returns 返回处理过的分页各项参数, 以及pagefunc函数(该函数用于处理切换页码/页容量逻辑, 封装了onclick)
 */
function commpage(curPageIndex, curPageNum, totalSize, pageIndexName, pageNumName, onclick){
	if(null==pageIndexName || ""==pageIndexName)
		pageIndexName = "pageIndex";
	
	if(null==pageNumName || ""==pageNumName)
		pageNumName = "pageNum";

	if(null==curPageIndex || ""==curPageIndex)
		curPageIndex = 0;
	else
		curPageIndex = parseInt(curPageIndex, 10);
	if(curPageIndex<0) curPageIndex = 0;
	
	if(null==curPageNum || ""==curPageNum)
		curPageNum = 0;
	else
		curPageNum = parseInt(curPageNum, 10);
	if(curPageNum<0) curPageNum = 0;
	
	if(null==totalSize || ""==totalSize)
		totalSize = 0;
	else
		totalSize = parseInt(totalSize, 10); 
	if(totalSize<0) totalSize = 0;
	
	var maxPageIndex = curPageNum<=0? 0 : parseInt((totalSize-1)/curPageNum);
	if(curPageIndex>maxPageIndex) curPageIndex = maxPageIndex;
	
	var prePageIndex = curPageIndex-1;
	var nextPageIndex = curPageIndex+1;
	if(prePageIndex<0) prePageIndex = 0;
	if(nextPageIndex>maxPageIndex) nextPageIndex = maxPageIndex;
	
	var start = curPageIndex*curPageNum;
	var end = start + curPageNum;
	if(end>0){
		start += 1;
	}
	if(end > totalSize) end = totalSize;
	
	pagefunc = function($this, event) {
		event.preventDefault();
		event.stopImmediatePropagation();
		event.stopPropagation();
		
		var pageindex = $this.data(pageIndexName);
		var pagenum = $this.data(pageNumName);
		var hasindex = (null!=pageindex && ""!==pageindex);
		var hasnum = (null!=pagenum && ""!==pagenum);
		if( (!hasindex && !hasnum)
			|| (hasindex && !hasnum && curPageIndex==pageindex)
			|| ( !hasindex && hasnum && pagenum==curPageNum)
			|| (hasindex && hasnum && curPageIndex==pageindex && pagenum==curPageNum)){
			return false;
		}
		
		if(null==onclick){
			var url = window.location.href;
			
			if(null!=pagenum && ""!==pagenum ){
				pagenum = parseInt(pagenum, 10);
				reg =new RegExp(pageNumName+"=\\w*", "g");
				if(url.match(reg)){
					url = url.replace(reg, pageNumName+"="+pagenum);
				}
				else{
					if(url.indexOf("?")<0){
						url += "?";
					}
					url += "&"+pageNumName+"="+pagenum;
				}
			}
			
			if(null!=pageindex && ""!==pageindex){
				pageindex = parseInt(pageindex, 10);
				var reg =new RegExp(pageIndexName+"=\\w*", "g");
				if(url.match(reg)){
					url = url.replace(reg, pageIndexName+"="+(pageindex+1));
				}
				else{
					if(url.indexOf("?")<0){
						url += "?";
					}
					url += "&"+pageIndexName+"="+(pageindex+1);
				}
			}
			window.location.href = url;
		}
		else{
			try{
				onclick(pageindex, pagenum);
			}catch(e){}
		}
		return true;
	};
	
	return {"curPageIndex":curPageIndex, "curPageNum":curPageNum, "totalSize":totalSize, "maxPageIndex":maxPageIndex, 
		"prePageIndex":prePageIndex,"nextPageIndex":nextPageIndex,"start":start, "end":end, 
		"pageIndexName":pageIndexName,"pageNumName":pageNumName,"pagefunc":pagefunc};
}

/**
 * 专用于loan crm的分页函数.
 * @param curPageIndex
 * @param curPageNum
 * @param totalSize
 * @param pageIndexName
 * @param pageNumName
 * @param onclick
 */
function yhqpage(curPageIndex, curPageNum, totalSize, pageIndexName, pageNumName, onclick){
	var pageparam = commpage(curPageIndex, curPageNum, totalSize, pageIndexName, pageNumName, onclick);
	curPageIndex = pageparam.curPageIndex;
	curPageNum = pageparam.curPageNum;
	var pagefunc = pageparam.pagefunc;
	
	$("#firstPageBtn, #prevPageBtn, #nextPageBtn, #lastPageBtn").click(function(event){
		return pagefunc($(this), event);
	});

	$("#spancustomercount").text(pageparam.totalSize);
	$("#first_rows").text(pageparam.start);
	$("#last_rows").text(pageparam.end);
	
	$("#firstPageBtn").data(pageparam.pageIndexName, 0);
	$("#lastPageBtn").data(pageparam.pageIndexName, pageparam.maxPageIndex);
	$("#prevPageBtn").data(pageparam.pageIndexName, pageparam.prePageIndex);
	$("#nextPageBtn").data(pageparam.pageIndexName, pageparam.nextPageIndex);
	
	var $pageindexsel = $("#pageindex");
	for(var i=0; i<=pageparam.maxPageIndex; ++i){
		$pageindexsel.append("<option value='"+i+"' "+((pageparam.curPageIndex==i)?"selected='selected'":"")+">"+(i+1)+"</option>");
	}
	$pageindexsel.data(pageparam.pageIndexName, pageparam.curPageIndex);
	$pageindexsel.change(function(event){
		$pageindexsel.data(pageparam.pageIndexName, $pageindexsel.val());
		return pagefunc($(this), event);
	});
	
	$("#pageallnums").text(pageparam.maxPageIndex+1);
	
	var $pagenumsel = $("#PageSize");
	$pagenumsel.val(pageparam.curPageNum);
	$pagenumsel.data(pageparam.pageNumName, pageparam.curPageNum);
	$pagenumsel.change(function(event){
		$pagenumsel.data(pageparam.pageNumName, $pagenumsel.val());
		return pagefunc($(this), event);
	});
}

//另外一个构造分页的函数, 此函数不介意使用, 可能会修改并过期
//构造分页. $elem:分页区块的容器区块,为jquery对象; pageIndex:当前页码[基于0]; pageNum: 每页数目;
//totalSize:总数;
//onclick:点击某一页时触发事件时的回调函数,原型function(pageIndex), 其中pageIndex表示目的页
function pagination($elem, pageIndex, pageNum, totalSize, onclick){
	if(null==pageIndex || ""==pageIndex)
		pageIndex = 0;
	else
		pageIndex = parseInt(pageIndex, 10);
	if(null==pageNum || ""==pageNum)
		pageNum = 0;
	else
		pageNum = parseInt(pageNum, 10);
	if(null==totalSize || ""==totalSize)
		totalSize = 0;
	else
		totalSize = parseInt(totalSize, 10); 
	
	if(null==$elem) return;
	var maxPageIndex = pageNum<=0? 0 : parseInt((totalSize-1)/pageNum);
	if(pageIndex>maxPageIndex) pageIndex = maxPageIndex;
	$elem.append(
			 '<div id="__pagination" style="position: relative; color: #FFFFFF; font-size:14px; width:100%; text-align:center;">'
			+'<div style="float:left;height: 20px;line-height: 25px;margin:0 0 0 10px"><span>共<span id="__total-size" style="padding:1px 5px;"></span>个<span></div>'
			+'<div style="float: left; background:url('+_JS_DIR+'img/separator.gif) no-repeat left; width:2px; height:25px; margin: 0px 20px;"></div>'
			+'<a id="__prepage" class="" href="#" style="margin-right:10px;text-decoration:underline;color: #FFFFFF;">上页</a>'
			+'<span id="__currentpage" style="margin:0px 5px;"></span>'
			+'<a id="__nextpage" class="" href="#"style="margin-left:10px;text-decoration:underline;color: #FFFFFF;">下页</a>'
			+'<span style="margin-left:15px;padding:3px 6px;">到第<input type="text" size="5" style="width:30px;" />页'
			+'<a id="__go" href="#" style="margin-left:5px;text-decoration:underline;color: #FFFFFF;">跳</a></span>'
			+'</div>');
	
	var pagediv = $("#__pagination");
	var prepage = $("#__prepage");
	var nextpage = $("#__nextpage");
	
	$("#__total-size").text(totalSize);
	
	// 上一页
	if(0<pageIndex){
		prepage.attr("value", pageIndex-1);
	}else{
		prepage.remove();
	}
	
	// 当前页
	var curpage = $("#__currentpage");
	curpage.text(pageIndex+1);
	
	// 下一页
	if(pageIndex<maxPageIndex){
		nextpage.attr("value", (pageIndex+1));
	}else{
		nextpage.remove();
	}
	
	// 跳转页输入框
	var $input = $("#__pagination input");
	$input.keyup(function() {
		var value = $(this).val();
		if(value==null||""==value||!isFinite(value)){
			$("#__go").attr("href", "#");
		}
		else{
			$("#__go").attr("value", value-1);
		}
	});
	// #region 获取跳转输入框中候选项
	var pagedata =[]; // 跳转输入框中候选项
	pagedata.push({name:"0", value:"1"});
	if(pageIndex-4>1)
		pagedata.push({name:"", value:"..."});
	var start = (pageIndex-4>0)?(pageIndex-4):1;
	var end = (pageIndex+4<maxPageIndex)?(pageIndex+4):(maxPageIndex-1);
	for(var i=start; i<=end; ++i){
		var page = i+1;
		if(i==pageIndex)
			pagedata.push({name:"", value:page});
		else{
			pagedata.push({name:i, value:page});
		}
	}
	if(pageIndex+4<maxPageIndex-1)
		pagedata.push({name:"", value:"..."});
	if(maxPageIndex>0)
		pagedata.push({name:maxPageIndex, value:(maxPageIndex+1)});
	// #endreg 获取跳转输入框中候选项
	
	extinput($input, function(qstr){
		return pagedata;
	},null,function(){
		$("#__pagination a").click();
	});
	
	
	$("#__pagination a").click(function(event) {
		event.preventDefault();
		event.stopImmediatePropagation();
		event.stopPropagation();
		
		$this = $(this);
		var pageindex = $(this).attr("value");
		if(null==pageindex || ""==pageindex){
			if("__go" == $this.attr('id'))
				pageindex = $this.prev().attr("value");
		}
		$(this).removeAttr("href");
		
		if(null==onclick){
			var url = window.location.href;
			var reg = /(&?)pageindex=\d*/g;
			if(url.match(reg)){
				url = url.replace(reg, "$1pageindex="+pageindex);
			}
			else{
				if(url.indexOf("?")<0){
					url += "?";
				}
				url += "&pageindex="+pageindex;
			}
			window.location.href = url;
		}
		else{
			try{
				onclick(pageindex);
			}catch(e){}
		}
		return true;
	});
}